package org.xfy.pet.service.impl;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xfy.base.query.BaseQuery;
import org.xfy.base.service.impl.BaseServiceImpl;
import org.xfy.base.util.AjaxResult;
import org.xfy.base.util.PageList;
import org.xfy.org.domain.Employee;
import org.xfy.org.domain.LoginData;
import org.xfy.org.domain.Logininfo;
import org.xfy.org.mapper.EmployeeMapper;
import org.xfy.pet.domain.Pet;
import org.xfy.pet.domain.PetDetail;
import org.xfy.pet.mapper.PetDetailMapper;
import org.xfy.pet.mapper.PetMapper;
import org.xfy.pet.query.PetQuery;
import org.xfy.pet.service.IPetService;
import org.xfy.sys.domain.User;
import org.xfy.sys.mapper.UserMapper;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;

    @Override
    public PageList<Pet> queryPage(PetQuery query, LoginData loginData) {
        //再查询当前页的分页数据
        Logininfo logininfo = loginData.getLogininfo();
        Employee employee = employeeMapper.findOneByLogininfo(logininfo.getId());
        if(employee!=null){
            query.setShopId(employee.getShopId());
        }
        //先查询总数量
        Long count = petMapper.queryCount(query);
        if(count < 1){//在没有数据的情况下可以提高查询效率
            return new PageList<Pet>();
        }
        List<Pet> list = petMapper.queryPage(query);
        PageList<Pet> pageList = new PageList<>();
        pageList.setData(list);
        pageList.setTotals(count);
        //返回查询结果
        return pageList;
    }

    @Override
    @Transactional
    public void add(Pet pet, LoginData loginData) {
        Logininfo logininfo = loginData.getLogininfo();
        Employee employee = employeeMapper.findOneByLogininfo(logininfo.getId());
        pet.setState(0);
        pet.setUserId(employee.getId());
        pet.setShopId(employee.getShopId());
        super.add(pet);
        PetDetail petDetail1 = pet.getPetDetail();
        PetDetail petDetail = new PetDetail();
        petDetail.setIntro(petDetail1.getIntro());
        petDetail.setAdoptNotice(petDetail1.getAdoptNotice());
        petDetail.setPetId(pet.getId());
        petDetailMapper.add(petDetail);
    }

    @Override
    public AjaxResult onsale(Long[] ids) {
        List<Pet> pets= petMapper.findPetsByIds(ids);
        Date date = new Date();
        for (Pet pet : pets) {
            if(pet.getState()==1){
                return AjaxResult.me().setMessage(pet.getName()+"该商品已上架").setSuccess(false);
            }
            pet.setState(1);
            pet.setOnsaletime(date);
            super.update(pet);
        }
        return AjaxResult.me();
    }

    @Override
    public AjaxResult offsale(Long[] ids) {
        List<Pet> pets= petMapper.findPetsByIds(ids);
        Date date = new Date();
        for (Pet pet : pets) {
            if(pet.getState()==0){
                return AjaxResult.me().setMessage(pet.getName()+"该商品已下架").setSuccess(false);
            }
            pet.setState(0);
            pet.setOffsaletime(date);
            super.update(pet);
        }
        return AjaxResult.me();
    }
}
